#include "EffectMgr.h"
#include "EffectEntry.h"
#include "Common/CharacterDefine.h"
#include "Character/Part.h"
#include "Character/Player.h"
#include "base/core/com_define.h"
#include "Common/TableData/EffectEffectCfg.h"


EffectManager::EffectManager()
{
	m_pEffects.clear();
}

EffectManager::~EffectManager()
{

}

bool EffectManager::Init()
{
	//注册效果类
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_Buff] = dynamic_cast<IEffect*>(new EffectAddBuff());
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_Mission] = dynamic_cast<IEffect*>(new EffectTriggerMission());
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_RefreshMonst] = dynamic_cast<IEffect*>(new EffectTriggerRefreshMonster());
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_Collect] = dynamic_cast<IEffect*>(new EffectTriggerRefreshCollect());
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_Plane] = dynamic_cast<IEffect*>(new EffectTriggerRefreshPlane());
	m_pEffects[(uint32_t)EEffectFuncType::EEffectFuncType_Trance] = dynamic_cast<IEffect*>(new EffectTriggerTrance());
	return true;
}

void EffectManager::UnInit()
{
	for (EffectMap::iterator iter = m_pEffects.begin(); iter != m_pEffects.end(); ++iter)
	{
		if (nullptr != iter->second)
		{
			MMO_DELETE(iter->second);
		}
	}

	m_pEffects.clear();
}

int32_t EffectManager::OnEffect(Creature *pCreatrue, EffectIDType effectId)
{
	const EffectEffectCfgInfo* effectCfgInfo = g_GetEffectEffectCfgTable()->GetEffectEffectCfgInfo(effectId);
	if (nullptr == effectCfgInfo)
	{
		return RET_FAIL;
	}


	if (effectCfgInfo->effectType <= (int32_t)EEffectFuncType::EEffectFuncType_None || effectCfgInfo->effectType >= (int32_t)EEffectFuncType::EEffectFuncType_Limit)
	{
		return RET_FAIL;
	}

	if (nullptr == m_pEffects[effectCfgInfo->effectType])
	{
		return RET_FAIL;
	}

	return m_pEffects[effectCfgInfo->effectType]->OnEffect(pCreatrue, effectCfgInfo);
}

int32_t EffectManager::RemoveEffect(Creature *pCreatrue, EffectIDType effectId)
{
	const EffectEffectCfgInfo* effectCfgInfo = g_GetEffectEffectCfgTable()->GetEffectEffectCfgInfo(effectId);
	if (nullptr == effectCfgInfo)
	{
		return RET_FAIL;
	}


	if (effectCfgInfo->effectType <= (int32_t)EEffectFuncType::EEffectFuncType_None || effectCfgInfo->effectType >= (int32_t)EEffectFuncType::EEffectFuncType_Limit)
	{
		return RET_FAIL;
	}

	if (nullptr == m_pEffects[effectCfgInfo->effectType])
	{
		return RET_FAIL;
	}

	return m_pEffects[effectCfgInfo->effectType]->RemoveEffect(pCreatrue, effectCfgInfo);
}